package com.demo643_子数组的最大平均数1;

public class Solution {
    public static void main(String[] args) {
        int[] ints={1,12,-5,-6,50,3};
        double maxAverage = findMaxAverage(ints, 4);
        System.out.println(maxAverage);
    }
    public static double findMaxAverage(int[] nums, int k) {
        int left =0;
        int right=0;
        if (k>nums.length){
            return 0;
        }
        double sum=0;
        double maxAvg=0;
        while (right<=nums.length-1){
            if (right<k){

                sum+=nums[right];
                right++;
                if (right==k-1){
                    maxAvg=sum/k;
                }
            }else {
                sum=sum-nums[left]+nums[right];
                maxAvg=Math.max(maxAvg,sum/k);
                left++;
                right++;
            }
        }

        return maxAvg;
    }
}
